package com.cslg.questionnaires.utils;

import com.cslg.questionnaires.entity.Question;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class QuestionUtils {
    /**
     * 处理前端构造 问卷表单的数据
     * 构建一个question对象，对前端传回的json进行编码
     * 主要是对content进行构造方便数据库存储
     * @param map
     * @param owerid
     * @return
     * @throws ParseException
     */
    public static Question buildQuestionForm(HashMap<String, Object> map, int owerid) throws ParseException {
        Question questionForm = new Question();
        // 标题
        String title = TextXssClean.cleanXSS((String)map.get("title"));

        // 日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String start_time = ((String)map.get("starttime")).replace("T"," ");
        String finish_time = ((String)map.get("finishtime")).replace("T"," ");

        Date starttime = sdf.parse(start_time);
        Date finishtime = sdf.parse(finish_time);

        ArrayList questions = (ArrayList)map.get("questions");
        String content = "";
        StringBuilder stringBuilder = new StringBuilder(content);
        for(int i = 0;i < questions.size();i ++){
            HashMap<String, Object> onequestion = (HashMap<String, Object>) questions.get(i);
            String type = TextXssClean.cleanXSS((String)onequestion.get("type"));
            String the_title = TextXssClean.cleanXSS((String)onequestion.get("title"));
            switch (type){
                case "oneChoose":
                case "moreChoose":
                    stringBuilder.append(type);
                    stringBuilder.append("><");
                    stringBuilder.append(the_title);
                    stringBuilder.append("''");
                    ArrayList options = (ArrayList)onequestion.get("options");
                    for (int j = 0;j < options.size();j ++){
                        stringBuilder.append("''");
                        stringBuilder.append(TextXssClean.cleanXSS((String) options.get(j)));
                    }
                    stringBuilder.append("<<>>");
                    break;
                case "oneLineText":
                case "moreLineText":
                case "recommendPerson":
                    stringBuilder.append(type);
                    stringBuilder.append("><");
                    stringBuilder.append(the_title);
                    stringBuilder.append("<<>>");
                    break;
            }
        }
        content = stringBuilder.toString();
        questionForm.setTitle(title);
        questionForm.setOwerid(owerid);
        questionForm.setStarttime(starttime);
        questionForm.setFinishtime(finishtime);
        questionForm.setContent(content);
        return questionForm;
    }

    /**
     * 解析 question问卷的内容部分，既构造问题部分
     * 通过json格式返回给前端
     * 返回列表list
     * oneChoose,moreChoose:{
     *     "type":"oneChoose",
     *     "title":"问题",
     *     "options":["选项","选项","选项"]
     * }
     * oneLineText,moreLineText,recommendPerson：{
     *     "type":"oneLineText"
     *     "title":"问题"
     * }
     * @param content
     * @return
     */
    public static ArrayList parseQuestionContent(String content){
        ArrayList contentQuestions = new ArrayList();
        String[] questions = content.split("<<>>");
        for (String question: questions){
            HashMap<String,Object> oneQuestion = new HashMap<>();

            String[] question_msg = question.split("><");
            String type = question_msg[0];
            String title;
            oneQuestion.put("type",type);
            switch (type){
                case "oneChoose":
                case "moreChoose":
                    String[] title_options = question_msg[1].split("''''");
                    oneQuestion.put("title",title_options[0]);
                    String[] options = title_options[1].split("''");
                    oneQuestion.put("options",options);
                    break;
                case "oneLineText":
                case "moreLineText":
                case "recommendPerson":
                    title = question_msg[1];
                    oneQuestion.put("title",title);
                    break;
            }
            contentQuestions.add(oneQuestion);
        }
        return contentQuestions;
    }
}
